/******************************************************************************
*	platform/arm/broadcom2835.c
*	 by Alex Chadwick
*
*	A light weight implementation of the USB protocol stack fit for a simple
*	driver.
*
*	platform/arm/broadcom2835.c contains code for the broadcom2835 chip, used
*	in the Raspberry Pi. Compiled conditionally on LIB_BCM2835=1.
******************************************************************************/
#include <configuration.h>
#include <platform/platform.h>
#include <types.h>

//+=============================================================================
// Func: Bcm2835Load
// Desc:
// Args: -none-
// Retn: -none-
// Note:
// Auth: Chadderz; BlueChip
//==============================================================================
void Bcm2835Load (void)
{
	LOG_DEBUG("CSUD: Broadcom2835 driver version 0.1.\n");
}

//+=============================================================================
// Func: MicroDelay
// Desc: Delays for 'delay' microseconds.
// Args: u32 delay .. Number of microseconds (uS) to delay
// Retn: -none-
// Note:
// Auth: Chadderz; BlueChip
//==============================================================================
#ifndef TYPE_DRIVER
void  MicroDelay (u32 delay)
{
	volatile u64  *timeStamp = (u64*)0x20003004;
	         u64  stop       = *timeStamp + delay;

	while (*timeStamp < stop)  __asm__("nop") ;
}
#endif // TYPE_DRIVER

//+=============================================================================
// Func: PowerOnUsb
// Desc: Turns on the USB host controller.
// Args: -none-
// Retn: Result ..
// Note: Notifies the parent system that the USB contorller now requires power.
// Auth: Chadderz; BlueChip
//==============================================================================
#ifndef TYPE_DRIVER
Result  PowerOnUsb (void)
{
	volatile u32  *mailbox;
	         u32  result;

	mailbox = (u32*)0x2000B880;

	while (mailbox[6] & 0x80000000) ;

	mailbox[8] = 0x80;
	do {
		while (mailbox[6] & 0x40000000) ;
		result = mailbox[0];
	} while ((result & 0xf) != 0);

	return (result == 0x80) ? OK : ErrorDevice;
}
#endif // TYPE_DRIVER

//+=============================================================================
// Func: PowerOffUsb
// Desc: Turns off the USB host controller.
// Args: -none-
// Retn: Result ..
// Note: Notifies the parent system that the USB contorller no longer requires
//         power.
// Auth: Chadderz; BlueChip
//==============================================================================
#ifndef TYPE_DRIVER
Result  PowerOffUsb (void)
{
	LOG_DEBUG("CSUD: PowerOffUsb() Not Implemented.\n");
	return ErrorNotImplemented;
}
#endif // TYPE_DRIVER
